package handler;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
//import java.io.*;
//import java.util.*;
import structures.Notice;
import structures.Paragraph;
import structures.Picture;

/**
 * NoticeHandler
 * 
 * @author Jose Antonio Prieto
 */
public class NoticeHandler extends DefaultHandler {

	/** Notice */
	private Notice notice;
	/** Accumulator */
	private StringBuffer accumulator;

	/**
	 * Constructor
	 */
	public NoticeHandler() {
		notice = new Notice();
	}

	/**
	 * Constructor
	 * 
	 * @param ph
	 */
	public NoticeHandler(Notice ph) {
		notice = ph;
	}

	/**
	 * Called at the beginning
	 */
	public void startDocument() {
		accumulator = new StringBuffer();
	}

	/**
	 * When the parser encounters plain text (not XML elements), it calls this
	 * method, which accumulates the text in a string buffer. Note that this
	 * method may be called in multiple times.
	 * 
	 * @param buffer
	 * @param start
	 * @param length
	 */
	public void characters(char[] buffer, int start, int length) {
		accumulator.append(buffer, start, length);
	}

	/**
	 * At the beginning of each new element, erase any accumulated text and
	 * capture attributes
	 * 
	 * @param namespaceURL
	 * @param localName
	 * @param qname
	 * @param attributes
	 */
	public void startElement(String namespaceURL, String localName,
			String qname, Attributes attributes) {
		accumulator.setLength(0);
		if (localName.equals("news")) {
			notice.setId(attributes.getValue("id"));
			notice.setOwner(attributes.getValue("owner"));
		}
	}

	/**
	 * Take special action when reach the end of selected elements. In this case
	 * the accumulated text is stored in the image object
	 * 
	 * @param namespaceURL
	 * @param localName
	 * @param qname
	 */
	public void endElement(String namespaceURL, String localName, String qname) {

		if (localName.equals("title")) {
			notice.setTitle(accumulator.toString().trim());
		} else if (localName.equals("id")) {
			notice.setId(accumulator.toString().trim());
		} else if (localName.equals("paragraph")) {
			Paragraph pa = new Paragraph(accumulator.toString().trim());
			notice.getNc().add(pa);
		} else if (localName.equals("picture")) {
			Picture pi = new Picture(accumulator.toString().trim());
			notice.getNc().add(pi);
		} else if (localName.equals("URL")) {
			notice.addURL(accumulator.toString().trim());
		}
	}

	/**
	 * Called at the end
	 */
	public void endDocument() {
	}

	/**
	 * Warning
	 * 
	 * @param exception
	 */
	public void warning(SAXParseException exception) {
		System.err.println("WARNING: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/**
	 * Error
	 * 
	 * @param exception
	 */
	public void error(SAXParseException exception) {
		System.err.println("ERROR: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/**
	 * Fatal error
	 * 
	 * @param exception
	 * @throws org.xml.sax.SAXException
	 */
	public void fatalError(SAXParseException exception) throws SAXException {
		System.err.println("FATAL: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
		throw (exception);
	}
}
